package com.startasone.service.Impl;

import com.startasone.mapper.DeptLogMapper;
import com.startasone.mapper.DeptMapper;
import com.startasone.mapper.EmpMapper;
import com.startasone.pojo.Dept;
import com.startasone.pojo.DeptLog;
import com.startasone.pojo.MyLog;
import com.startasone.service.DeptLogService;
import com.startasone.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private DeptLogService deptLogService;
    @Override
    public void update(Dept dept) {
        deptMapper.update(dept);
    }

    @Override
    @MyLog
    public List<Dept> list() {
        return deptMapper.list();
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    @MyLog
    public void delete(Integer id) throws Exception {
        try {
            empMapper.deleteByDeptId(id);
            deptMapper.delete(id);
        } finally {
            DeptLog deptLog=new DeptLog();
            deptLog.setDeptId(id);
            deptLog.setDescription("删除的部门id为{}"+id);
            deptLogService.createDeptLog(deptLog);
            System.out.println("日志成功");
        }
    }

    @MyLog
    @Override
    public void save(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.save(dept);
    }

    @Override
    public Dept getById(Integer id) {

        return deptMapper.getById(id);
    }

}
